/**
 * Created by asus on 2017/4/28.
 */
import http from '../../../common/httpUtil'
import * as types from './mutationTypes'

const actions = {
  querySysAffiches({commit}, {index = 1, size = 10} = {}) {
    commit(types.AFFICHE_QUERY_AFFICHES_BEGIN)
    let url = `sys/affiche?page=${index}&size=${size}`;
    return http.getRaw(url).then(result => {
      let affiches = result.body.objs;
      let currentPage = result.body.page;
      let totalCount = result.body.totalNum;
      //获取置顶项有新接口
      /*
      let upItem = [];
      let notUpItem = [];
      for (let k = 0; k < affiches.length; k++) {
        if (affiches[k].isUp === true) {
          upItem.push(affiches[k]);
        } else {
          notUpItem.push(affiches[k]);
        }
      }
      affiches = upItem.concat(notUpItem);
      */
      commit(types.AFFICHE_QUERY_AFFICHES_SUC, {affiches, currentPage, totalCount})
    })
  },
  queryUpAffiches({commit}) {
    //获取置顶项
    let url = 'sys/affiche/up';
    return http.getRaw(url).then(result => {
      let affiches = result.body.objs;
      commit(types.AFFICHE_QUERY_UP_SUC, {affiches})
    })
  },
  deleteAffiche({commit}, params) {
    /*
     let data = {
     courseId: params.courseId
     };
     */
    let url = `sys/affiche?ids=${params.id}`;
    return http.delete(url).then(() => {
      commit(types.AFFICHE_DELETE_SUC)
    })
  },
  batchDeleteAffiches({commit}, params) {
    let ids = [];
    for (let k = 0; k < params.length; k++) {
      ids.push(params[k].id);
    }
    let query = ids.join(',');
    let url = `sys/affiche?ids=${query}`;
    return http.delete(url).then(() => {
      commit(types.AFFICHE_DELETE_SUC)
    })
  },
  setEditingAffiche({commit}, affiche) {
    //{}
    commit(types.AFFICHE_SET_EDITING_AFFICHE, {affiche})
  },
  setPageModel({commit}, modelType) {
    //''
    commit(types.AFFICHE_SET_PAGE_MODEL, {modelType})
  },
  releaseAffiche({commit}, affiche) {
    let url = 'sys/affiche';
    return http.post(url, affiche).then(data => {
      commit(types.AFFICHE_RELEASE_SUC)
    })
  },
  updateAffiche({commit}, affiche) {
    let url = 'sys/affiche';
    return http.post(url, affiche).then(() => {
      commit(types.AFFICHE_UPDATE_SUC)
    });
  }
}
export default actions
